package com.jmb.apiplatform.controller;

import com.jmb.apiplatform.common.LoginUserInfo;
import com.jmb.apiplatform.common.PageResult;
import com.jmb.apiplatform.common.Result;
import com.jmb.apiplatform.exception.BusinessException;
import com.jmb.apiplatform.exception.ValidException;
import com.jmb.apiplatform.pojo.*;
import com.jmb.apiplatform.service.ApiTestCaseExecuteLogService;
import com.jmb.apiplatform.service.ApiTestCaseService;
import com.jmb.apiplatform.util.NoUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "接口测试用例")
@RequestMapping("/api/test/case")
@RestController
public class ApiTestCaseController {
    @Autowired
    private ApiTestCaseService apiTestCaseService;
    @Autowired
    private LoginUserInfo loginUserInfo;

    @Autowired
    private ApiTestCaseExecuteLogService logService;
    @PostMapping("save")
    public Result saveApiTestCase(@RequestBody ApiTestCaseTO apiTestCaseTO) throws ValidException {
        // 我们默认传0，是新增
        apiTestCaseTO.setSource(0);
        apiTestCaseTO.setImportNo(null);
        apiTestCaseService.saveApiTestCaseInfo(apiTestCaseTO);
        return Result.success("新增成功");
    }
    @GetMapping("list")
    public Result findApiTestCaseList(ApiTestCaseListTO apiTestCaseListTO) throws ValidException {
        PageResult<ApiTestCaseListVO> apiTestCaseList = apiTestCaseService.findApiTestCaseList(apiTestCaseListTO);
        return Result.success(apiTestCaseList);
    }

    @GetMapping("info/{caseId}")
    public Result findApiTestCaseInfo(@PathVariable Integer caseId) throws ValidException {
        ApiTestCaseInfoVO apiTestCaseInfoVO = apiTestCaseService.editApiTestCaseId(caseId);
        return Result.success(apiTestCaseInfoVO);
    }
    @PostMapping("modify")
    public Result editApiTestCase(@RequestBody ApiTestCaseTO apiTestCaseTO) throws ValidException {
        apiTestCaseService.editApiTestCase(apiTestCaseTO);
        return Result.success("修改成功");
    }
    @GetMapping("remove/{caseId}")
    public Result removeApiTestCase(@PathVariable Integer caseId) throws ValidException {
        apiTestCaseService.removeApiTestCase(caseId);
        return Result.success("删除成功");
    }
    // 执行接口测试用例
    @GetMapping("execute/{caseId}")
    public Result executeInterfaceCase(@PathVariable Integer caseId, HttpServletRequest request) throws ValidException {
        String executor = loginUserInfo.getRealName(request);
        try {
            Integer executeLog = apiTestCaseService.executeInterfaceCase(new ExecuteInterfaceCaseParam(caseId, executor,
                    null, NoUtil.genChainNo(), null,  1,
                    null, null, null, null, 0, null, false));
            Integer status = logService.findExecute(executeLog).getStatus();
            if (status == 0) {
                return Result.success("执行成功");
            } else if (status == 1) {
                return Result.fail("执行失败");
            } else {
                return Result.fail("执行错误");
            }
        } catch (BusinessException e) {
            return Result.fail("执行错误");
        }
    }



}
